package com.qqqq.service.impl;

import com.qqqq.code.BusinessException;
import com.qqqq.code.Page;
import com.qqqq.dao.IUserDao;
import com.qqqq.domain.User;
import com.qqqq.service.IUserService;
import com.qqqq.utils.MD5Utils;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements IUserService {

    private final IUserDao userDao;

    public UserServiceImpl(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User login(String username, String password) throws SQLException {
        User target = this.userDao.getUserByUserName(username,password);
        if (target == null) {
            throw new BusinessException(500, "您输入的用户不存在！");
        }

        if (!target.getPassword().equals(/*MD5Utils.md5(MD5Utils.md5(*/password))/*))*/ {
            throw new BusinessException(500, "您输入的密码有误");
        }

        return target;
    }



    @Override
    public int addUser(User user) throws SQLException {
        return this.userDao.addUser(user);
    }

    @Override
    public int deleteUser(int id) throws SQLException {
        return this.userDao.deleteStudent(id);
    }

    @Override
    public int updateUser(User user) throws SQLException {
        return this.userDao.updateUser(user);
    }

    @Override
    public User getUser(int id) throws SQLException {
         return this.userDao.getUser(id);
    }

    @Override
    public Page<User> getUserPage(int pageNum, int pageSize, User user) throws SQLException {
        int total=this.userDao.count(user);
        List<User> userList=this.userDao.page(user,pageNum,pageSize);
        return new Page<>(total,userList);
    }
}
